package com.example.android_wx.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatEditText;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatTextView;

import com.example.android_wx.R;
import com.example.android_wx.base.App;
import com.example.android_wx.base.BaseActivity;
import com.example.android_wx.fragment.MineFragment;
import com.example.android_wx.http.BaseCallBack;
import com.example.android_wx.http.HttpHelp;
import com.jeremyliao.liveeventbus.LiveEventBus;

public class ModifyNameActivity extends BaseActivity {
    private AppCompatImageView backImageView;
    private AppCompatEditText newNameEditText;
    private AppCompatTextView modifyTextView;
    private View  newNameView;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_modify_name);
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initView() {

        backImageView = findViewById(R.id.backImageView);
        modifyTextView = findViewById(R.id.modifyTextView);
        newNameEditText = findViewById(R.id.newNameEditText);
        newNameView = findViewById(R.id.newNameView);


    }

    @Override
    public void initEvent() {


        backImageView.setOnClickListener(view -> finish());

        newNameView.setOnFocusChangeListener((view, b) -> {
            if (b) {
                newNameView.setBackgroundResource(R.color.primary);

            }
        });

        modifyTextView.setOnClickListener(v -> {

            String nickname = newNameEditText.getText().toString();
            if (nickname.isEmpty()){
                Toast.makeText(this, "请输入名称", Toast.LENGTH_SHORT).show();
                return;
            }

            modifyTextView.setEnabled(false);//禁用按钮
            modifyTextView.setText("正在修改...");//修改按钮文字

            HttpHelp.get().ready("member", "modifyNickname")
                    .add("nickname", nickname)
                    .get(new BaseCallBack() {
                        @Override
                        public void onSuccess(String data) {

                            App.get().getMemberData().setMemberNickname(nickname);

                            Toast.makeText(ModifyNameActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                            finish();
                        }

                        @Override
                        public void onFailure(String reason) {
                            Toast.makeText(ModifyNameActivity.this, reason, Toast.LENGTH_SHORT).show();
                            modifyTextView.setEnabled(true);//启用按钮
                            modifyTextView.setText("保存");//修改按钮文字
                        }
                    });


        });



    }
}
